﻿
namespace WPF.TestEditor.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using MahApps.Metro;
    using System.Collections;

   
    public static class ThemeManager
    {     
        private static IEnumerable<Accent> _accents;
        public static IEnumerable<Accent> DefaultAccents
        {
            get
            {
                return _accents ?? (_accents =
                    new List<Accent>{
                        new Accent("Red", new Uri("pack://application:,,,/Themes/Styles/Red.xaml")),
                        new Accent("Green", new Uri("pack://application:,,,/Themes/Styles/Green.xaml")),
                        new Accent("Blue", new Uri("pack://application:,,,/Themes/Styles/Blue.xaml")),
                        new Accent("Purple", new Uri("pack://application:,,,/Themes/Styles/Purple.xaml")),
                        new Accent("Orange", new Uri("pack://application:,,,/Themes/Styles/Orange.xaml")),
                        new Accent("Bottle-Glass", new Uri("pack://application:,,,/Themes/Styles/BootleGlass.xaml")),
                        new Accent("Visual Studio 2012 (Light)", new Uri("pack://application:,,,/Themes/Styles/VisualStudio2012Light.xaml"))
                    });
            }
        }
        public static Accent DefaultSkin = new Accent("Metro", new Uri("pack://application:,,,/Themes/Skins/metro.xaml"));

        public static void ChangeTheme(Application app, Accent accent)
        {
            ChangeTheme(app.Resources, DefaultSkin);
            ChangeTheme(app.Resources, accent);
            ChangeTheme(app.Resources, DefaultSkin);
            ChangeTheme(app.Resources, accent);     
        }

        public static void ChangeTheme(Window window, Accent accent)
        {
            ChangeTheme(window.Resources, DefaultSkin);
            ChangeTheme(window.Resources, accent);
            ChangeTheme(window.Resources, DefaultSkin);
            ChangeTheme(window.Resources, accent);
        }

        public static void ChangeTheme(ResourceDictionary r, Accent accent)
        {   
            ApplyResourceDictionary(accent.Resources, r);
        }

        private static void ApplyResourceDictionary(ResourceDictionary newRd, ResourceDictionary oldRd)
        {
            foreach (DictionaryEntry r in newRd)
            {
                if (oldRd.Contains(r.Key)) oldRd.Remove(r.Key);
                oldRd.Add(r.Key, r.Value);
            }
        }
    }
}
